package club.kingyin.kyblogprouser.service.impl;

import club.kingyin.commons.clients.CoreClient;
import club.kingyin.commons.entity.user.User;
import club.kingyin.commons.enums.BlogEnum;
import club.kingyin.kyblogprouser.entity.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import club.kingyin.kyblogprouser.dao.FriendDao;
import club.kingyin.commons.entity.user.Friend;
import club.kingyin.kyblogprouser.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Friend)表服务实现类
 *
 * @author makejava
 * @since 2022-01-20 15:04:00
 */
@Transactional
@Service("friendService")
public class FriendServiceImpl extends ServiceImpl<FriendDao, Friend> implements FriendService {

    @Resource
    private FriendDao friendDao;

    @Autowired
    private CoreClient coreClient;

    public UserInfo info(Integer id) {
        UserInfo userInfo = new UserInfo();
        LambdaQueryWrapper<Friend> queryWrapper = Wrappers.lambdaQuery();
        // 关注
        queryWrapper.eq(Friend::getId, id).in(Friend::getStatus,BlogEnum.CLOSELY,BlogEnum.CONCERNED);
        Integer like = friendDao.selectCount(queryWrapper);
        queryWrapper.clear();
        // 粉丝
        queryWrapper.eq(Friend::getId, id).in(Friend::getStatus,BlogEnum.CLOSELY,BlogEnum.BE_CONCERNED);
        Integer fans = friendDao.selectCount(queryWrapper);
        Integer views = (Integer) coreClient.getViewsById(id).getData();
        userInfo.setFans(fans);
        userInfo.setLikes(like);
        userInfo.setViews(views);
        return userInfo;
    }

    public BlogEnum getStatusByIdAndFriendId(int id, int friendId) {
        if (id == friendId)
            return BlogEnum.SELF;
        LambdaQueryWrapper<Friend> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Friend::getId, id).eq(Friend::getFriendid, friendId).select(Friend::getStatus);
        Friend friend = friendDao.selectOne(queryWrapper);
        if (friend == null)
            return BlogEnum.NORMAL;
        return friend.getStatus();
    }


    public BlogEnum changeFriendByIdAndFriendId(int id, int friendId) {
        LambdaQueryWrapper<Friend> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Friend::getId, id).eq(Friend::getFriendid, friendId)
        .select(Friend::getStatus);
        Friend status = friendDao.selectOne(queryWrapper);
        LambdaUpdateWrapper<Friend> updateWrapper = Wrappers.lambdaUpdate();
        int res;
        if (status != null) {
            // 有关系
            switch (status.getStatus()) {
                // 已关注
                case CONCERNED:
                    // 删除关系
                    updateWrapper.eq(Friend::getId,id).eq(Friend::getFriendid,friendId).or().eq(Friend::getId,friendId).eq(Friend::getFriendid,id);
                    res = friendDao.delete(updateWrapper);
                    if (res == 2)
                        return BlogEnum.NORMAL;
                    break;
                // 被关注
                case BE_CONCERNED:
                    // 关注他 -> 互相关注
                    updateWrapper.clear();
                    updateWrapper.eq(Friend::getId,id).eq(Friend::getFriendid,friendId).or().eq(Friend::getId,friendId).eq(Friend::getFriendid,id);
                    updateWrapper.set(Friend::getStatus, BlogEnum.CLOSELY);
                    res = friendDao.update(null, updateWrapper);
                    if (res == 2)
                        return BlogEnum.CLOSELY;
                    break;
                // 互相关注
                case CLOSELY:
                    updateWrapper.clear();
                    updateWrapper.eq(Friend::getId,id).eq(Friend::getFriendid,friendId).set(Friend::getStatus,BlogEnum.BE_CONCERNED);
                    res = friendDao.update(null, updateWrapper);
                    updateWrapper.clear();
                    updateWrapper.eq(Friend::getId,friendId).eq(Friend::getFriendid,id).set(Friend::getStatus,BlogEnum.CONCERNED);
                    res += friendDao.update(null, updateWrapper);
                    if (res == 2)
                        return BlogEnum.BE_CONCERNED;
                    break;
            }
        } else {
            // 没有关系
            Friend friend = new Friend();
            friend.setId(id);
            friend.setFriendid(friendId);
            friend.setStatus(BlogEnum.CONCERNED);
            res = friendDao.insert(friend);
            friend.setId(friendId);
            friend.setFriendid(id);
            friend.setStatus(BlogEnum.BE_CONCERNED);
            res += friendDao.insert(friend);
            if (res == 2)
                return BlogEnum.CONCERNED;
        }
        return BlogEnum.DELETE;
    }


}

